<?php

class Zend_View_Helper_BuildMenu
{
	public function buildMenu($base_url, $level_id, $selected_controller = '', $selected_action = '')
	{
			// load registry, translate and access level object
			$registry = Zend_Registry::getInstance();
			$translator = $registry->get('Zend_Translate');

			$access_levels = $registry->get('access_level_config');
			// load xml from menu file
			$xml = new SimpleXMLElement('../application/menu.xml', NULL, true);
			foreach($xml->level as $level){
				if($level['id'] != $level_id){ // check the level id to find out the menu to display
					continue;
				}
				// there is a menu for this level build it
				$html_menus = array();
				foreach($level->menu as $menu){ // traverse through all menus
					$html_menu = "<div id=\"menu_name\" >" . "<div class =\"textGray\">" . $translator->translate((string)$menu['name']). "</div><div class=\"drop\"></div></div>";
					$html_menu .= "<div class=\"Sap_side\"></div>";
					$html_submenus = array();
					// build submenu array of html
					foreach($menu->submenu as $submenu){
						$html_submenu = '';
						if($submenu['depth'] == "1"){
							$html_submenu .= "<div id=\"menu_item\"><div class=\"redArrow\"></div><div class =\"textGreen\"><a  href=\"" . $base_url . "/" . $submenu['controller'] . "/" . $submenu['action'] . "\">" . $translator->translate((string)$submenu['name']) . "</a></div></div>";
							$html_submenu .= "<div class=\"Sap_side\"></div>";
							$html_submenus[((int)$submenu["order"])-1] = $html_submenu;

						}
					}
					// sort the submenu array by the specified order
					for($i = 0; $i < count($html_submenus); $i++){
						if(isset($html_submenus[$i]))$html_menu .= $html_submenus[$i];
					}

					$html_menus[((int)$menu["order"])-1] = $html_menu;
				}
				for($i = 0; $i < count($html_menus); $i++){
						if(isset($html_menus[$i]))$menu .= $html_menus[$i];
				}
				return $menu;
			}

	}
}
?>